package com.artPreter.utils;

import com.artPreter.pojo.AlertMemo;
import com.artPreter.pojo.TempHumMemo;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


public class FileUtil {

    public static String writeAlertMemo(List<AlertMemo> alertMemoTxtList) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < alertMemoTxtList.size(); i++) {
            AlertMemo memo = alertMemoTxtList.get(i);
            String rows = memo.getCreateTime() + "|"
                    + (memo.getIsOver().equals(0) ? "正常" : "异常") + "|"
                    + memo.getRemarks() + "|"
                    + memo.getDeviceId() + "|"
                    + memo.getFloor() + "|"
                    + memo.getRoom() + "|";
            list.add(rows);

        }
        if (list.size() > 0) {
            String path = createpath();
            writeTxt(path,list,2);
        }
        return "";
    }

    public static String writeTempHumMemo(List<TempHumMemo> tempHumMemoList) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < tempHumMemoList.size(); i++) {
            TempHumMemo memo = tempHumMemoList.get(i);
            String rows = memo.getCreateTime() + "|"
                    + memo.getTempVal()+ "|"
                    + (memo.getOverValue().equals("0") ? "正常" : "异常") + "|"
                    + memo.getHumVal()+ "|"
                    + (memo.getOverValue2().equals("0") ? "正常" : "异常") + "|"
                    + memo.getRemarks() + "|"
                    + memo.getDeviceId() + "|"
                    + memo.getFloor() + "|"
                    + memo.getRoom() + "|";
            list.add(rows);
        }
        if (list.size() > 0) {
            String path = createpath();
            writeTxt(path,list,1);
        }
        return "";
    }

    /**
     * 创建/写入TXT
     *
     * @return
     */
    public static String writeTxt(String path, List<String> list, Integer type) {
        FileWriter fw = null;
        try {
            //如果文件存在，则追加内容；如果文件不存在，则创建文件
            File f = new File(path + "\\" + type + ".txt");
            fw = new FileWriter(f, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter pw = new PrintWriter(fw);
        for (int i = 0; i < list.size(); i++) {
            pw.println(list.get(i));
        }

        pw.flush();
        try {
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }


    public static String createpath() {
        String separator = File.separator;

        // 服务器nginx位置
        String serverPath = "D:\\java\\uploads";

        // 年月日文件夹
        LocalDateTime now = LocalDateTime.now();
        String dayStr = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String[] dayArr = dayStr.split("-");
        String dateDir = separator + dayArr[0] + separator + dayArr[1] + separator + dayArr[2];
        String path = serverPath + dateDir;

        String year = dayArr[0];
        String month = dayArr[1];

        String yearDir = serverPath + File.separator + year;
        File yearFile = new File(yearDir);
        if (!yearFile.exists()) {
            yearFile.mkdirs();
        }

        String monthDir = yearDir + File.separator + month;
        File monthFile = new File(monthDir);
        if (!monthFile.exists()) {
            monthFile.mkdirs();
        }

        String dayDir = path;
        File dayFile = new File(dayDir);
        if (!dayFile.exists()) {
            dayFile.mkdirs();
        }
        String result = serverPath + separator + year + separator + month + separator + dayArr[2] + separator;
        return result;
    }

}
